<?php
class Unit_1T{

	/**
	 * Create a 1 Tile unit
	 *
	 * @param array $request
	 * @return array
	 */
	public function Create(array $request){
		require_once(DirectoryManager::$pathCore."/Database.php");

		//Convert params to db friendly array
		$params = array(
			$request['B'],
			$request['TYP'],
			$request['P'],
			$request['x'],
			$request['y'],
			$request['r'],
		);

		//Peform DB action
		$db = Database::getInstance();
		$rows = $db->callSP(SiteConfig::sP_Unit1T_Create, $params);
		$rows = $rows[0];
		//VERY basic error checking
		If($rows And $rows[0] != -1){
			//Convert results to return friendly
			$return = array(
			"U" => $rows['NewUnitId'],
			"TYP" => $rows['TargetUnitTypeId'],
			"P" => $rows['TargetPlayerId'],
			"T" => $rows['NewTeamId'],
			"at" => $rows['NewActiveTurn'],
			"c" => $rows['NewCallsign'],
			"f" => $rows['NewFootprint'],
			"x" => $rows['CoordX'],
			"y" => $rows['CoordY'],
			"r" => $rows['Rotation'],
			"hp" => $rows['NewHP'],
			"sp" => $rows['NewSP'],
			"mp" => $rows['NewMoveAP'],
			"ap" => $rows['NewAttackAP'],
			"MT" => $rows['NewMovementType'],
			"mlt" => $rows['NewMovedLastTurn'],
			"W1" => $rows['NewWeaponId1'],
			"W2" => $rows['NewWeaponId2'],
			"W3" => $rows['NewWeaponId3'],
			"W4" => $rows['NewWeaponId4'],
			"W5" => $rows['NewWeaponId5'],
			"W6" => $rows['NewWeaponId6'],
			"wa1" => $rows['WeaponAmmo1'],
			"wa2" => $rows['WeaponAmmo2'],
			"wa3" => $rows['WeaponAmmo3'],
			"wa4" => $rows['WeaponAmmo4'],
			"wa5" => $rows['WeaponAmmo5'],
			"wa6" => $rows['WeaponAmmo6']
			);

			return $return;
		}Else{
			throw new exception("Unexpected error in 'Unit1T > Create'");
		}
	}

	/**
	 * Move and/or rotate a 1 T Unit
	 *
	 * @param array $request
	 * @return array
	 */
	public static function Move(array $request){
		require_once(DirectoryManager::$pathCore."/Database.php");
		# Convert params to db friendly array
		$params = array($request['U'],$request['x'],$request['y'],$request['r']);

		# Peform DB action
		$db = Database::getInstance();
		$rows = $db->callSP(SiteConfig::sP_Unit1T_Move, $params);

		# VERY basic error checking
		If($rows){
			If($rows[0] != -1){
				$rows = $rows[0];
				# Convert results to return friendly
				$return = array(
				"U" => $rows['Id'],
				"mp" => $rows['APMove']
				);
				return $return;

			}Else{
				# Move failed
				$return = array(-1);
				return $return;
			}

		}Else{
			throw new exception("Unexpected error in 'Unit1T > Move'");
		}
	}

	/**
	 * Calculate attack by a 1 tile unit
	 *
	 * @param array $request ['U'=Firing UnitId, 'W'= WeaponId, 'x'=Target coord X, 'y'=Target coord Y, 'TU'=TargetUnit Id]
	 * @return array $rows [All unit rows affected by the attack, including the attacker]
	 */
	public static function Attack(array $request){
		require_once(DirectoryManager::$pathCore."/Database.php");
		# Convert params to db friendly array
		$params = array($request['U'],$request['W'],$request['x'],$request['y'],$request['TU']);

		# Peform DB action
		$db = Database::getInstance();
		$rows = $db->callSP(SiteConfig::sP_Unit1T_Attack, $params);
		$rows = $rows[0];

		# VERY basic error checking
		Try{
			$return = array();
			If($rows And $rows[0] != -1){
				//Convert result to return friendly
				foreach($rows as &$row){
					$convertedRow = array(
					"U" => $row["Id"],
					"TYP" => $row["UnitTypeId"],
					"P" => $row["PlayerId"],
					"T" => $row["TeamId"],
					"at" => $row["ActiveTurn"],
					"MT" => $row["MovementType"],
					"mlt" => $row["MovedLastTurn"],
					"c" => $row["UnitCallsign"],
					"f" => $row["UnitTile"],
					"x" => $row["X"],
					"y" => $row["Y"],
					"r" => $row["UnitTileBearing"],
					"hp" => $row["HP"],
					"sp" => $row["SP"],
					"mp" => $row["APMove"],
					"ap" => $row["APAttack"],
					"W1" => $row["Weapon1Id"],
					"W2" => $row["Weapon2Id"],
					"W3" => $row["Weapon3Id"],
					"W4" => $row["Weapon4Id"],
					"W5" => $row["Weapon5Id"],
					"W6" => $row["Weapon6Id"],
					"a1" => $row["Ammo1"],
					"a2" => $row["Ammo2"],
					"a3" => $row["Ammo3"],
					"a4" => $row["Ammo4"],
					"a5" => $row["Ammo5"],
					"a6" => $row["Ammo6"]
					);

					array_push($return, $convertedRow);
				}
//				//### DEBUG ###
//				$debug = "DEBUG: ";
//				foreach($rows As $key => $value){
//					$debug = $debug."[".$key."]=".print_r($value,true);
//				}
//				throw new exception($debug);
				return $return;

			}Else{
				$return = array(-1);
				return $return;
			}
		}Catch(Exception $e){
			//TODO Marc: Do we want to silence all errors and just return this?
			throw new exception("Unexpected error in 'Unit1T > Attack'");
		}
	}

}
?>